<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use app\admin\model\School as schoolModel;//学校信息模型
use app\admin\model\AdminMenu;
use app\admin\controller\User;
class School extends User
{
    /**
     * 学校信息列表
     * @return [type] [description]
     */
    public function index()
    {
        //实例化学校模型
        $model = new schoolModel();
        $xname='';
        $post = $this->request->post();
        if (isset($post['xname']) and !empty($post['xname'])) {
            $where['xname'] = ['like', '%' . $post['xname'] . '%'];
            $xname=$post['xname'];
        }
        $school = empty($where) ? $model->order('xid asc')->paginate(4) : $model->where($where)->order('xid asc')->paginate(4);

        $this->assign('school',$school);
        $this->assign('xname',$xname);
        return $this->fetch();
    }
    /**
     * 学校的添加及修改
     * @return [type] [description]
     */
    public function publish()
    {
        //获取学校id
        $xid = $this->request->has('xid') ? $this->request->param('xid', 0, 'intval') : 0;
        $model = new schoolModel();
        if($xid > 0) {
            //是修改操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['xname', 'require', '学校名称不能为空'],
                ]);
                //验证部分数据合法性
                if (!$validate->check($post)) {
                    $this->error('提交失败：' . $validate->getError());
                }
                //验证学校名是否存在
                $xname = $model->where(['xname'=>$post['xname']])->select();
                if(!empty($xname)) {
                    return $this->error('提交失败：该学校名称已经存在');
                }
                if(false == $model->allowField(true)->save($post,['xid'=>$xid])) {
                    return $this->error('修改失败');
                } else {
                    addlog($model->id);//写入日志
                    return $this->success('修改学校信息成功','admin/school/index');
                }
            } else {
                //非提交操作
                $info['school'] = $model->where('xid',$xid)->find();
                $this->assign('info',$info);
                return $this->fetch();
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                //验证  唯一规则： 表名，字段名，排除主键值，主键名
                $validate = new \think\Validate([
                    ['xname', 'require','学校名称不能为空']
                ]);
                //验证用户名是否存在
                $name = $model->where('xname',$post['xname'])->select();
                if(!empty($name)) {
                    return $this->error('提交失败：该学校名已被注册');
                }
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加学校失败');
                } else {
                    //addlog($model->id);//写入日志
                    return $this->success('添加学校成功','admin/school/index');
                }
            } else {
                //非提交操作
                return $this->fetch();
            }
        }
    }
    /**
     * 学校删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('school')->where('xid',$id)->delete()) {
                return $this->error('删除失败');
            } else {
                addlog($id);//写入日志
                return $this->success('删除成功','admin/school/index');
            }
        }
    }
}
?>